package com.sk.weichat.adapter;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.weichat.R;
import com.sk.weichat.bean.Friend;
import com.sk.weichat.helper.AvatarHelper;
import com.sk.weichat.ui.base.CoreManager;
import com.sk.weichat.util.AppUtils;
import com.sk.weichat.util.HtmlUtils;
import com.sk.weichat.util.StringUtils;
import com.sk.weichat.util.TimeUtils;
import com.sk.weichat.view.HeadView;
import java.util.List;

public class SearchChatRecodeAdapter extends BaseQuickAdapter<Friend, BaseViewHolder> {

    public String keyword;

    private boolean isHideTime = false;//是否隐藏时间
    public SearchChatRecodeAdapter( List<Friend> data,boolean isHideTime) {
        super(R.layout.item_search_chat_recode, data);
        this.isHideTime = isHideTime;
    }

    @Override
    protected void convert(BaseViewHolder helper, Friend item) {
        helper.addOnClickListener(R.id.layout_search_friend_item);
        ImageView imgAvatar = helper.getView(R.id.img_search_avatar);
        HeadView avatar_imgS = helper.getView(R.id.avatar_imgS);
        TextView tv_time = helper.getView(R.id.tv_time);

        TextView tv_chant_content = helper.getView(R.id.tv_chant_content);
        TextView tv_nike_name = helper.getView(R.id.tv_nike_name);
        if (item.getRoomFlag()==0){
            imgAvatar.setVisibility(View.VISIBLE);
            avatar_imgS.setVisibility(View.GONE);
            //头像
            AvatarHelper.getInstance().displayAvatar("", item.getUserId(),imgAvatar, false);
        }else {
            imgAvatar.setVisibility(View.GONE);
            avatar_imgS.setVisibility(View.VISIBLE);
            AvatarHelper.getInstance().displayAvatar(CoreManager.requireSelf(mContext).getUserId(), item, avatar_imgS);
        }
        if (TextUtils.isEmpty(item.getRemarkName())){
            tv_nike_name.setText(item.getNickName());
        }else {
            tv_nike_name.setText(item.getRemarkName());
        }
        if (isHideTime){
            tv_time.setVisibility(View.GONE);
        }else {
            tv_time.setVisibility(View.VISIBLE);
            tv_time.setText(TimeUtils.getFriendlyTimeDesc(mContext, item.getTimeSend()));
        }
        if (item.getContextNum()<2){
            String text = item.getContent();
            //判断内容是否是表情
            if (HtmlUtils.isMesageToSmilis(text)){
                String s = StringUtils.replaceSpecialChar(text);
                CharSequence content = HtmlUtils.transform200SpanString(s.replaceAll("\n", "\r\n"), true);
                // TODO: 这样匹配的话正常消息里的&8824也会被替换掉，
                if (content.toString().contains("&8824")) {// 草稿
                    content = content.toString().replaceFirst("&8824", "");
                }
                tv_chant_content.setText(content);
            }else {
                SpannableStringBuilder style = AppUtils.TextColorkeywordByBule(mContext,item.getContent(),getKeyword());
                tv_chant_content.setText(style!=null?style:item.getContent());
            }

        }else {
            tv_chant_content.setText(item.getContextNum()+"条相关记录信息");
        }
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }
}
